//js 调用
$(function(){
   //弹窗
   $('#myModal').modal();
   var acctAddr;
   //构造web3对象
   var web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
   web3.eth.getAccounts(function(e,r){
      console.log(e,r);
   });
   //构造crowd合约对象
   var crowdContractAddr = "0x259272473a8899032a6afff4a01eb7f9319a1df9";
   var ownerAddr = "0x5a61e77544bb569cf7e5d2739a03db1e21b49aff";
   var crowdObj = new web3.eth.Contract(crowdABI, crowdContractAddr);
   


   //get insideList
   var wizzContractAddr;
   var mvcContractAddr;
   var wizzObj;
   var mvcObj;
   function getInsideList(){
      crowdObj.methods.insideList().call(function(error,result){
         if(!error){
            //获取wizz,mvc合约地址
            wizzContractAddr = result.wizzContractAddr;
            mvcContractAddr = result.mvcContractAddr;
            console.log("get wizz n mvc addr:",wizzContractAddr,mvcContractAddr);
            //获取wizz,mvc合约对象
            wizzObj = new web3.eth.Contract(wizzABI, wizzContractAddr);
            mvcObj = new web3.eth.Contract(mvcABI, mvcContractAddr);
         }
      });

   }
   getInsideList();

   //登陆按钮被点击
   $(".Login").on("click",function(){
      acctAddr = $("#addressId").val();
      console.log("get acct:",acctAddr);
      $(".close_win").click();
   });


   //充值wizz(空投)
   $(".Recharge").on("click",function(){
      wizzObj.methods.airDrop(acctAddr,1000).send({
         from : ownerAddr,        //owner
         gas  : 300000
      },function(error,result){
         if(!error){
            alert("充值成功！");
         }else{
            alert("充值失败！");
            console.log("充值失败:",error);
         }
      });
   });


   //投票处理
   $(".Vote").on("click",function(){
      //调用wizz的转账给owner,至少1000wizz 
      wizzObj.methods.transfer(ownerAddr,1000).send({
         from : acctAddr,
         gas  : 300000
      },function(error,result){
         if(!error){
            console.log("transfer ok");
            //调用mvc的空投给acctAddr
            mvcObj.methods.airDrop(acctAddr,1000).send({
               from : ownerAddr,
               gas  : 300000
            },function(error,result){
               if(!error){
                  alert("投票成功！");
               }else{
                  alert("投票失败！");
                  console.log("投票失败:",error);
               }
            });
         }
      });  
   });


   //刷新mvc数量，wizz余额，投票时间，众筹进度
   $(".Refresh").on("click",function(){
      //mvc数量,投票时间
      mvcObj.methods.crowInfo(acctAddr).call(function(error,result){
         if(!error){
            document.getElementById("mvc").innerHTML = result.amount;
            document.getElementById("time").innerHTML = result.crowdTime;
         }else{
            console.log("get crowInfo error:",error);
         }
      });
      //wizz余额
      wizzObj.methods.balanceOf(acctAddr).call(function(error,result){
         if(!error){
            document.getElementById("wizz").innerHTML = result;
         }else{
            console.log("get balanceOf error:",error);
         }
      });
      //众筹进度
      mvcObj.methods.totalCrowd().call(function(error,result){
         if(!error){
            var totalCrowd = result;            //目前发行量
            mvcObj.methods.totalSupply().call(function(error,result){
               if(!error){
                  var totalSupply = result;     //总发行量
                  //众筹进度
                  document.getElementById("percent").innerHTML = totalCrowd/totalSupply*100;
               }else{
                  console.log("get totalSupply error:",error);
               }
            });
         }else{
            console.log("get totalCrowd error:",error);
         }
      });

   });


});